package com.dep.aspect;

import com.dep.common.CodeIdEnum;
import com.dep.util.BizException;
import com.dep.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Slf4j
@Aspect
@Component
public class TokenAspect {
    @Resource
    private HttpServletRequest request;


    @Before("execution(* com.dep.controller..*.*(..)) " +
            "&& @annotation(com.dep.aspect.Token)")
    public void doAround(JoinPoint pjp) throws Exception {
        checkToken(pjp);
    }
    private void checkToken(JoinPoint pjp) throws Exception {
        String uri = this.request.getRequestURI();
        if (StringUtil.isNullorEmpty(uri)) {
            throw new BizException(CodeIdEnum.Expired);
        }
        String[] uriarray = uri.split("/");
        if (uriarray.length < 2) {
            throw new BizException(CodeIdEnum.Expired);
        }

        String token = this.request.getHeader("token");
        String sign = this.request.getHeader("sign");
        //进行token校验

      /*  if(StringUtil.isNullorEmpty(token) || !token.equals("test")){
            throw new BizException(CodeIdEnum.Expired);
        }*/

/*        if(StringUtil.isNullorEmpty(sign) ){
            throw new BizException(CodeIdEnum.SIGNNULL);
        }

        if(!sign.equals("test")){
            throw new BizException(CodeIdEnum.SIGNERRO);
        }*/

        //校验失败
/*        if (StringUtil.isNullorEmpty(this.request.getAttribute("infoId"))) {
            throw new BizException(CodeIdEnum.Expired);
        }*/


    }

}
